package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.BoilHops;
import Models.DryHops;
import Models.MaltBill;
import Models.Recipe;
import Models.RecipeAdditives;

public class RecipePresenter extends AbstractPresenter<Recipe> {
	final String SINGLEITEM = "recipes";
	final String MULTIITEM = "recipess";
	
	public ArrayList<Recipe> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Recipe tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Recipe tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			Recipe item = createItemFromJSONObject(jsonHop);
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Recipe createItemFromJSONObject(JSONObject jsonObject){
		Recipe item = new Recipe();
		try{
			item.setId(jsonObject.getInt("rid"));
			item.setRevision(jsonObject.getInt("RRev"));
			item.setIbus(jsonObject.getDouble("ribu"));
			item.setPrimaryTemp(jsonObject.getDouble("PTemp"));
			item.setVolume(jsonObject.getDouble("RVol"));
			item.setTargetGravity(jsonObject.getDouble("rgt"));
			item.setName(jsonObject.getString("RName"));
			item.setRecipeNotes(jsonObject.getString("notes"));
			
			if(!jsonObject.isNull("yeastName")){
				//create product if set
				YeastPresenter presenter = new YeastPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("yeastName");
				item.setYeastId(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setYeastId(null);
			}
			
			if(!jsonObject.isNull("mashSchedule")){
				//create product if set
				MashSchedulePresenter presenter = new MashSchedulePresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("mashSchedule");
				item.setMashSchedule(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setMashSchedule(null);
			}
			
			if(!jsonObject.isNull("recipeSaltID")){
				//create product if set
				RecipeSaltsPresenter presenter = new RecipeSaltsPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("recipeSaltID");
				item.setRecipeSalts(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setRecipeSalts(null);
			}
			
			//Set Boil Hops Collection
			ArrayList<BoilHops> boilHops = new ArrayList<BoilHops>();
			if(!jsonObject.isNull("boilHopCollection")){
				Object itm = jsonObject.get("boilHopCollection");
				BoilHopsPresenter presenter = new BoilHopsPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						boilHops.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					boilHops.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setBoilHops(boilHops);
			}
			
			//Set Dry Hops Collection
			ArrayList<DryHops> dryHops = new ArrayList<DryHops>();
			if(!jsonObject.isNull("dryHopCollection")){
				Object itm = jsonObject.get("dryHopCollection");
				DryHopsPresenter presenter = new DryHopsPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						dryHops.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					dryHops.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setDryHops(dryHops);
			}
			
			//Set Malt Bill Collection
			ArrayList<MaltBill> maltBills = new ArrayList<MaltBill>();
			if(!jsonObject.isNull("maltBillCollection")){
				Object itm = jsonObject.get("maltBillCollection");
				MaltBillPresenter presenter = new MaltBillPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						maltBills.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					maltBills.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setMaltBill(maltBills);
			}
			
			//Set Recipe Additives Collection
			ArrayList<RecipeAdditives> additives = new ArrayList<RecipeAdditives>();
			if(!jsonObject.isNull("additiveCollection")){
				Object itm = jsonObject.get("additiveCollection");
				RecipeAdditivesPresenter presenter = new RecipeAdditivesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						additives.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					additives.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setSpiceAndFlavors(additives);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Recipe item){
		try{
			JSONObject json = new JSONObject();
			json.put("rid", item.getId());
			json.put("RRev", item.getRevision());
			json.put("ribu", item.getIbus());
			json.put("PTemp", item.getPrimaryTemp());
			json.put("RVol", item.getVolume());
			json.put("rgt", item.getTargetGravity());
			json.put("RName", item.getName());
			json.put("notes", item.getRecipeNotes());

			if(item.getYeastId()!=null){
				final YeastPresenter presenter = new YeastPresenter();
				json.put("yeastName", presenter.createJSON(item.getYeastId()));
			} else {
				json.put("yeastName", JSONObject.NULL);
			}

			if(item.getMashSchedule()!=null){
				final MashSchedulePresenter presenter = new MashSchedulePresenter();
				json.put("mashSchedule", presenter.createJSON(item.getMashSchedule()));
			} else {
				json.put("mashSchedule", JSONObject.NULL);
			}

			if(item.getRecipeSalts()!=null){
				final RecipeSaltsPresenter presenter = new RecipeSaltsPresenter();
				json.put("recipeSaltID", presenter.createJSON(item.getRecipeSalts()));
			} else {
				json.put("recipeSaltID", JSONObject.NULL);
			}
			
			//Create boilHopCollection Collection
			if(item.getBoilHops()!=null){
				BoilHopsPresenter presenter = new BoilHopsPresenter();
				if(item.getBoilHops().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getBoilHops().size(); i++){
						jsonArray.put(presenter.createJSON(item.getBoilHops().get(i)));
					}
					json.put("boilHopCollection", jsonArray);
				}
			}
			
			//Create dryHopCollection Collection
			if(item.getDryHops()!=null){
				DryHopsPresenter presenter = new DryHopsPresenter();
				if(item.getDryHops().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getDryHops().size(); i++){
						jsonArray.put(presenter.createJSON(item.getDryHops().get(i)));
					}
					json.put("dryHopCollection", jsonArray);
				}
			}
			
			//Create maltBillCollection Collection
			if(item.getMaltBill()!=null){
				MaltBillPresenter presenter = new MaltBillPresenter();
				if(item.getMaltBill().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getMaltBill().size(); i++){
						jsonArray.put(presenter.createJSON(item.getMaltBill().get(i)));
					}
					json.put("maltBillCollection", jsonArray);
				}
			}
			
			//Create additiveCollection Collection
			if(item.getSpiceAndFlavors()!=null){
				RecipeAdditivesPresenter presenter = new RecipeAdditivesPresenter();
				if(item.getSpiceAndFlavors().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getSpiceAndFlavors().size(); i++){
						jsonArray.put(presenter.createJSON(item.getSpiceAndFlavors().get(i)));
					}
					json.put("additiveCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Recipe getEmptyEntity(){
		Recipe entity = new Recipe();
		return entity;
	}
}
